package com.lanju.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.lanju.entity.SystemAdmin;



@Repository
public class SystemAdminDao {

    private static final Log log = LogFactory.getLog(SystemAdminDao.class);

    @PersistenceContext(unitName = "financialmarketingUnit")
    private EntityManager entityManager;

    public void persist(SystemAdmin transientInstance) {
        try {
            entityManager.persist(transientInstance);
        } catch (RuntimeException re) {
            log.error("persist failed", re);
            throw re;
        }
    }

    public void remove(SystemAdmin persistentInstance) {
        try {
            entityManager.remove(persistentInstance);
        } catch (RuntimeException re) {
            log.error("remove failed", re);
            throw re;
        }
    }

    public SystemAdmin merge(SystemAdmin detachedInstance) {
        try {
            SystemAdmin result = entityManager.merge(detachedInstance);
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public SystemAdmin findById(Long id) {
        try {
            SystemAdmin instance = entityManager.find(SystemAdmin.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    @SuppressWarnings("unchecked")
	public SystemAdmin findByAdminCode(String adminCode) {
        try {
            Query query = entityManager.createQuery("from SystemAdmin where adminCode=?1").setParameter(1, adminCode);
            List<SystemAdmin> list = query.getResultList();
            if(list!=null && list.size()>0){
                return list.get(0);
            }else{
                return null;
            }
        } catch (Exception e) {
            log.error("findByAdminCode fail",e);
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
	public SystemAdmin findAdmin(String adminName) {
        try {
            Query query = entityManager.createQuery("from SystemAdmin where adminName=?1").setParameter(1, adminName);
            List<SystemAdmin> list = query.getResultList();
            if(list!=null && list.size()>0){
                return list.get(0);
            }else{
                return null;
            }
        } catch (Exception e) {
            log.error("findAdmin fail",e);
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
	public List<SystemAdmin> getAdminList(String string, int pageSize, int offset) {
        try {
            Query query = entityManager.createQuery(string.toString()).setFirstResult(offset).setMaxResults(pageSize);
            return query.getResultList();
        } catch (Exception e) {
            log.error("getAdminList fail",e);
            throw e;
        }
    }

    public long getAdminListCount(String string, int pageSize, int offset) {
        try {
            Query query = entityManager.createQuery(string.toString());
            return (long) query.getSingleResult();
        } catch (Exception e) {
            log.error("getAdminList fail",e);
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
	public boolean isAdmin(String adminCode) {
        try {
            Query query = entityManager.createQuery("from SystemAdmin where adminCode=?1").setParameter(1, adminCode);
            List<SystemAdmin> list = query.getResultList();
            if(list!=null && list.size()>0){
                return true;
            }else{
                return false;
            }
        } catch (Exception e) {
            log.error("isAdmin fail",e);
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
	public List<SystemAdmin> getOrganizeInAdminList(int flag) {
        try {
            Query query = entityManager.createQuery("from SystemAdmin s where s.flag=?1").setParameter(1, flag);
            return query.getResultList();
        } catch (Exception e) {
            log.error("getOrganizeInAdminList fail",e);
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
	public List<SystemAdmin> getList(String adminName, int pageSize, int offset) {
        try {
            StringBuffer sqld = new StringBuffer("from SystemAdmin s where s.adminName <>?1 ");
            if(!StringUtils.isEmpty(adminName)){
               sqld.append(" and s.adminname like:n ");
            }
            Query query = entityManager.createQuery(sqld.toString()).setFirstResult(offset).setMaxResults(pageSize).setParameter(1, "admin");
            if(!StringUtils.isEmpty(adminName)){
                query.setParameter("n", "%"+adminName+"%");
            }
            return query.getResultList();
        } catch (Exception e) {
            log.error("getList fail",e);
        }
        return null;
    }
    
    public long getListCount(String adminName) {
        try {
            StringBuffer sqld = new StringBuffer("select count(*) from SystemAdmin s where s.adminName <>?1");
            if(!StringUtils.isEmpty(adminName)){
               sqld.append(" and s.adminname like:n ");
            }
            Query query = entityManager.createQuery(sqld.toString()).setParameter(1, "admin");
            if(!StringUtils.isEmpty(adminName)){
                query.setParameter("n", "%"+adminName+"%");
            }
            return (long) query.getSingleResult();
        } catch (Exception e) {
            log.error("getList fail",e);
        }
        return 0;
    }
}
